package com.yd.app.service.pc.roleService;

import com.yd.app.mapper.RoleMapper;
import com.yd.app.service.common.BaseService;
import com.yd.domain.commom.PageModel;
import com.yd.domain.searchParam.RoleParam;
import com.yd.domain.sys.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * @author zy
 * @create 2018-03-07 15:41
 * @desc 角色管理
 **/
@Service
public class RoleService extends BaseService{
    @Autowired
    private RoleMapper roleMapper;
    /**
     * @Author: zy
     * @Desc:新增角色
     * @Date:  2018/3/7
     */
    public void addRole(Role role){
        roleMapper.addRole(role);
    }
    /**
     * @Author: zy
     * @Desc:根据code删除角色
     * @Date:  2018/3/7
     */
    @Transactional
    public void deleteRoleByCode(String code){
        roleMapper.deleteRoleByCode(code);//删除角色
        roleMapper.deleteUserRoleByRoleCode(code);//删除该角色相关的关系信息
        roleMapper.deleteRolePermByRoleCode(code);//删除角色权限关系信息
    }
    /**
     * @Author: zy
     * @Desc:修改角色
     * @Date:  2018/3/7
     */
    public void updateRole(Role role){
        roleMapper.updateRole(role);
    }
    /**
     * @Author: zy
     * @Desc:根据code查询角色
     * @Date:  2018/3/7
     */
    public Role getRoleByID(String code){
        Role r=new Role();
        r=roleMapper.getRoleByID(code);
        return r;
    }
    /**
     * @Author: zy
     * @Desc:分页检索角色数据
     * @Date:  2018/3/7
     */
    public PageModel getRoleList(RoleParam p, int pageIndex, int pageSize){
        PageModel pm=new PageModel();
        List<Role> list=new ArrayList<>();
        list=roleMapper.searchRoleList(p,getStartRow(pageIndex,pageSize),pageSize);
        int count=0;
        if(pageIndex==1){
            //查询总条数
            count=roleMapper.searchRoleCount(p);
            pm.setCount(count);
        }
        pm.setList(list);
        return pm;
    }
    /**
     * @Author: zy
     * @Desc: 获取角色已有的权限
     * @Date:  2018/3/7
     */
    public PageModel getPermListByRole(String roleId, int pageIndex, int pageSize) {
        PageModel pm=new PageModel();
        List<Perm>list= roleMapper.searchPermissionList(roleId,getStartRow(pageIndex,pageSize),pageSize);
        pm.setList(list);
        int count=0;
        if(pageIndex==1){
            //查询总条数
            count=roleMapper.searchPermissionCount(roleId);
            pm.setCount(count);
        }
        return pm;
    }
    /**
     * @Author: zy
     * @Desc:角色查询未有的权限
     * @Date:  2018/3/7
     */
    public PageModel getNotHavePermListByRole(String roleId,int pageIndex, int pageSize) {
        PageModel pm=new PageModel();
        List<Perm>list= roleMapper.searchNotHavePermissionList(roleId,getStartRow(pageIndex,pageSize),pageSize);
        pm.setList(list);
        int count=0;
        if(pageIndex==1){
            //查询总条数
            count=roleMapper.searchNotHavePermissionCount(roleId);
            pm.setCount(count);
        }
        return pm;
    }

    /**
     * @Author: zy
     * @Desc:角色添加权限
     * @Date:  2018/3/7
     */
    public void addPermToRole(List<RolePerm> list) {
        roleMapper.addPermToRole(list);
    }
    /**
     * @Author: zy
     * @Desc:角色删除权限
     * @Date:  2018/3/7
     */
    public void deletePermFromRole(RolePerm pr) {
        roleMapper.deletePermFromRole(pr);
    }
    /**
     * @Author: zg
     * @Desc:查询用户角色
     * @Date:  2018/3/7
     */
    public List<Role> getUserRole(String userId) {
        List<Role>list=roleMapper.getUserRole(userId);
        return list;
    }
    /**
     * 通过用户id查询用户权限信息
     * @param userId
     * @return
     */
    public List<Perm> getUserPerm(String userId) {
        List<Perm> list=roleMapper.getUserPerm(userId);
        return list;
    }

    /**
     * 添加用户角色
     * @param userRole
     */
    public void saveUserRole(UserRole userRole) {
        roleMapper.saveUserRole(userRole);
    }
    /**
     * 删除用户角色
     * @param userRole
     */
    public void deleteUserRole(UserRole userRole) {
        roleMapper.deleteUserRole(userRole);
    }


    public List<Role> getAllRoleList() {
        List<Role>list= roleMapper.getAllRoleList();
        return  list;
    }

    public void addUserRole(UserRole ur) {
        roleMapper.addUserRole(ur);
    }
}
